home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Games / xjewel / jewel.c < prev    next >
C/C++ Source or Header  |  1995-05-03  |  13KB  |  552 lines

  1. /*
  2. **
  3. **    X11 Jewel By David Cooper and Jose Guterman 05/92
  4. **
  5. */
  6.  
  7. #define XK_MISCELLANY
  8. #define XK_LATIN1
  9. #ifdef VMS
  10. #include <decw$include/Xlib.h>
  11. #include <decw$include/Xutil.h>
  12. #include <decw$include/Xos.h>
  13. #include <decw$include/keysymdef.h>
  14. #else
  15. #include <X11/Xlib.h>
  16. #include <X11/Xutil.h>
  17. #include <X11/Xos.h>
  18. #include <X11/keysymdef.h>
  19. #endif
  20.  
  21. #include <stdlib.h>
  22.  
  23. #include "general.h"
  24. #include "logic.h"
  25. #include "jewel.h"
  26. #include "xw.h"
  27. #include "xhscore.h"
  28. #include "hscore.h"
  29. #include "intro.h"
  30. #include "game.h"
  31. #include "panel.h"
  32. #include "help.h"
  33.  
  34. #include "version.h"
  35. char *StartString="Press \140Space\047 to begin, or \140H\047 for Help";
  36.  
  37. enum JStates JewelState;
  38. /* event functions */
  39.  
  40. void xw_focus_event(xfev)
  41. XFocusChangeEvent *xfev;
  42.     {
  43.     /* handle focus change */
  44.     if ((JewelState == GAME) && (xfev->type == FocusOut))
  45.         {
  46.         /*printf("FocusOut->PAUSED\n");*/
  47.         Set_Pause();
  48.         }
  49.     }
  50.  
  51.  
  52. void xw_leave_event(xlev)
  53. XLeaveWindowEvent *xlev;
  54.     {
  55. #ifdef LEAVE_PAUSE
  56.     if (JewelState == GAME)
  57.         {
  58.         /*printf("LeaveOut->PAUSED\n");*/
  59.         Set_Pause();
  60.         }
  61. #endif
  62.     }
  63.  
  64.  
  65. static enum JStates OldState=NULL_STATE;
  66.  
  67. static void StartIcon()
  68.     {
  69.     OldState=JewelState;
  70.     JewelState=ICON;
  71.     xw_set_timer(2000l);
  72.     }
  73.  
  74. static void EndIcon()
  75.     {
  76.     JewelState=OldState;
  77.     xw_set_timer(100L);
  78.     }
  79.  
  80. void xw_map_event(xmev)
  81. XMapEvent *xmev;
  82.     {
  83.     /*printf("GOT MAP EVENT. win=%ld, type=%d\n",xmev->window,
  84.     xmev->type);*/
  85.     if (xmev->window == xw_window)
  86.         {
  87.         if ((JewelState!=ICON) && (xmev->type == UnmapNotify))
  88.             {
  89.             StartIcon();
  90.             }
  91.         if ((JewelState==ICON) && (xmev->type == MapNotify))
  92.             {
  93.             EndIcon();
  94.             }
  95.         }
  96.     }
  97.  
  98. void xw_key_event(xkev)
  99. XKeyEvent *xkev;
  100.     {
  101.     /* deal with key event */
  102.     KeySym key;
  103.  
  104.     key=XLookupKeysym(xkev,xkev->state);
  105.     switch(JewelState)
  106.         {
  107.         case GAME:
  108.             switch(key)
  109.                 {
  110.                 /* HIDDEN KEYS USED FOR DEBUGGING 
  111.                 case XK_plus:
  112.                 case XK_KP_Add:
  113.                     Inc_Stage();    
  114.                     break;
  115.                 case XK_minus:
  116.                 case XK_KP_Subtract:
  117.                     Dec_Stage();    
  118.                     break; 
  119.                 ********************************/
  120.                 case XK_U:
  121.                 case XK_u:
  122.                 case XK_Menu:/* do */
  123.                 case XK_F1:
  124.                 case XK_F20:
  125.                     XIconifyWindow(xw_display, xw_window, xw_screen);
  126.                     break;
  127.                 case XK_Cancel:
  128.                 case XK_E:
  129.                 case XK_e:
  130.                     Melt_Down();
  131.                     End_Game();
  132.                     break;
  133.                 case XK_P:
  134.                 case XK_p:
  135.                     Toggle_Pause();
  136.                     break;
  137.                 case XK_S:
  138.                 case XK_s:
  139.                     Toggle_Sound();
  140.                     break;
  141.                 case XK_X:
  142.                 case XK_x:
  143.                 case XK_Q:
  144.                 case XK_q:
  145.                     printf("Thankyou for playing...try again sometime!\n");
  146.                     xw_exit_main();
  147.                     break;
  148.                 case XK_Left:
  149.                 case XK_4:
  150.                 case XK_KP_4:
  151.                 case XK_J:
  152.                 case XK_j:
  153.                     if (!Paused()) Move_Left();
  154.                     break;
  155.                 case XK_Right:
  156.                 case XK_6:
  157.                 case XK_KP_6:
  158.                 case XK_L:
  159.                 case XK_l:
  160.                     if (!Paused()) Move_Right();
  161.                     break;
  162.                 case XK_Next:
  163.                 case XK_Up:
  164.                 case XK_5:
  165.                 case XK_KP_5:
  166.                 case XK_K:
  167.                 case XK_k:
  168.                     if (!Paused()) Rotate();
  169.                     break;
  170.                 case XK_Down:
  171.                 case XK_0:
  172.                 case XK_KP_0:
  173.                 case XK_space:
  174.                 case XK_KP_Space:
  175.                     if (!Paused()) Drop();
  176.                     break;
  177.                 }
  178.             break;
  179.         case INTRO:
  180.         case HIGHSCORE:
  181.         case HELP:
  182.             switch (key)
  183.                 {
  184.                 case XK_Select:
  185.                 case XK_Execute:
  186.                 case XK_S:
  187.                 case XK_s:
  188.                 case XK_space:
  189.                 case XK_KP_Space:
  190.                     Start_New_Game();
  191.                     break;
  192.                 case XK_P:
  193.                 case XK_p:
  194.                     Set_State_High_Score();
  195.                     break;
  196.                 case XK_H:
  197.                 case XK_h:
  198.                 case XK_F1:
  199.                 case XK_Help:
  200.                 case XK_Menu:
  201.                     Start_Help();
  202.                     break;
  203.                 case XK_X:
  204.                 case XK_x:
  205.                 case XK_Q:
  206.                 case XK_q:
  207.                     printf("Thankyou for playing...try again sometime!\n");
  208.                     xw_exit_main();
  209.                     break;
  210.                 }
  211.             break;
  212.         }
  213.     XFlush(xw_display);
  214.     
  215.     /*xw_ev.x = xkev->x;
  216.     xw_ev.y = xkev->y;*/
  217.     }
  218.  
  219.  
  220.  
  221. void xw_but_event(xbev)
  222. XButtonEvent *xbev;
  223.     {
  224.     /* deal with key event */
  225.     /*printf("buttonpress:%d\n",xbev->button); */
  226.     /*xw_ev.button = xbev->button;
  227.     xw_ev.x = xbev->x;
  228.     xw_ev.y = xbev->y;*/
  229.     }
  230.  
  231.  
  232.  
  233. void xw_expose_event(xev)
  234. XExposeEvent *xev;
  235.     {
  236.     /* do screen update */
  237.     /*printf("GOT EXPOS EVENT, win=%ld, jstate=%d\n", xev->window,
  238.     JewelState);*/
  239. #ifdef ICON_WINDOW
  240.     if (xev->window != xw_window)
  241.         { /* must be icon window */
  242.         if (JewelState != ICON)
  243.         { StartIcon(); }
  244.         return;
  245.         }
  246.     else
  247.         {
  248.         if (JewelState == ICON)
  249.         { EndIcon(); }
  250.         }
  251. #endif
  252.     if (!(xev->count))
  253.         {
  254.         switch(JewelState)
  255.             {
  256.             case NULL_STATE:
  257.                 Start_Intro();
  258.                 break;
  259.             case GAME:
  260.                 Expose_Game();
  261.                 break;
  262.             case HELP:
  263.                 Expose_Help();
  264.                 break;
  265.             case INTRO:
  266.                 Expose_Intro();
  267.                 break;
  268.             case HIGHSCORE:
  269.                 Refresh_High_Scores();
  270.                 break;
  271.             }
  272.         }
  273.     }
  274.  
  275.  
  276. void Rot_Icons()
  277.     {
  278. #ifdef ICON_WINDOW
  279.     extern Window iw_window;
  280.     extern int iw_width, iw_height;
  281.     static int IconPiece=PIECE1;
  282.  
  283.     XCopyPlane(xw_display, PiecesPM[IconPiece], iw_window, PiecesGC[IconPiece],
  284.         0, 0, SIZE_PIECE, SIZE_PIECE,
  285.         (iw_width - SIZE_PIECE)/2, (iw_height - SIZE_PIECE)/2, 1L);
  286.  
  287.     IconPiece++;
  288.     if (IconPiece == NUM_REAL_PIECES) { IconPiece=PIECE1; }
  289. #endif
  290.  
  291.     xw_set_timer(2000L);
  292.     }
  293.  
  294. void xw_timeout()
  295.     {
  296.     /* handle timer */
  297.     switch(JewelState)
  298.         {
  299.         case GAME:
  300.             Game_Timeout();
  301.             break;
  302.         case HIGHSCORE:
  303.             Start_Help();
  304.             break;
  305.         case HELP:
  306.             Start_Intro();
  307.             break;
  308.         case INTRO:
  309.             Intro_Timeout();
  310.             break;
  311.         case ICON:
  312.             Rot_Icons();
  313.             break;
  314.         }
  315.     }
  316.  
  317.  
  318.  
  319. /********************************************************/
  320.  
  321. #include "bitmaps/jewellogo.xbm"
  322. #include "bitmaps/jewellogo2.xbm"
  323. /* border */
  324. #include "bitmaps/border.xbm"
  325. /*#include "bitmaps/border1.xbm"*/
  326. /* pieces */
  327. #include "bitmaps/piece1.xbm"
  328. #include "bitmaps/piece2.xbm"
  329. #include "bitmaps/piece3.xbm"
  330. #include "bitmaps/piece4.xbm"
  331. #include "bitmaps/piece5.xbm"
  332. #include "bitmaps/piece6.xbm"
  333. #include "bitmaps/jewel.xbm"
  334. #include "bitmaps/flash1.xbm"
  335. #include "bitmaps/flash2.xbm"
  336. #include "bitmaps/flash3.xbm"
  337. #include "bitmaps/flash4.xbm"
  338. /* backdrops */
  339. #include "bitmaps/back1.xbm"
  340. #include "bitmaps/back2.xbm"
  341. #include "bitmaps/back3.xbm"
  342. #include "bitmaps/back4.xbm"
  343.  
  344. int Border_Width=border_width;
  345. int Border_Height=border_height;
  346. int Jewellogo2_Width=jewellogo2_width;
  347. int Jewellogo2_Height=jewellogo2_height;
  348. int Jewellogo_Width=jewellogo_width;
  349. int Jewellogo_Height=jewellogo_height;
  350.  
  351. Pixmap LogoPM;
  352. Pixmap Logo2PM;
  353. GC LogoGC;
  354. GC Logo2GC;
  355.  
  356. Pixmap BorderPM;
  357. GC BorderGC;
  358.  
  359. Pixmap PiecesPM[NUM_PIECES];
  360. GC PiecesGC[NUM_PIECES];
  361.  
  362. GC ScoreGC;
  363. GC HighScoreGC;
  364. int Score_Char_MHeight;
  365. int Score_x_right;
  366. GC VerGC;
  367. XFontStruct *VerFont, *ScoreFont, *HighScoreFont;
  368.  
  369. Pixel colors[NUM_PIECES];
  370.  
  371. Pixel green;
  372. Pixel yellow;
  373. Pixel black;
  374. Pixel white;
  375.  
  376. void Init_Jewel()
  377.     {
  378.     XGCValues gcv;
  379.     unsigned long gcvm=(GCGraphicsExposures | GCForeground | GCBackground);
  380.  
  381.     green=xw_alloc_color("green");
  382.     yellow=xw_alloc_color("yellow");
  383.     black=BlackPixel(xw_display,xw_screen);
  384.     white=WhitePixel(xw_display,xw_screen);
  385.  
  386.     gcv.graphics_exposures=False;
  387.     gcv.background=black;
  388.  
  389.     /* Logo */
  390.     gcv.foreground=green;
  391.     LogoGC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  392.     LogoPM=XCreateBitmapFromData(xw_display, xw_window,
  393.             jewellogo_bits, jewellogo_width, jewellogo_height);
  394.     XSetClipMask(xw_display,LogoGC,LogoPM);
  395.  
  396.     gcv.foreground=white;
  397.     Logo2GC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  398.     Logo2PM=XCreateBitmapFromData(xw_display, xw_window,
  399.             jewellogo2_bits, jewellogo2_width, jewellogo2_height);
  400.  
  401.     /* border */
  402.     gcv.foreground=white;
  403.     BorderGC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  404.     BorderPM=XCreateBitmapFromData(xw_display, xw_window, border_bits, 
  405.             border_width, border_height);
  406.  
  407.     /* Pieces */
  408.     colors[WILD_PIECE]=xw_alloc_color(WILD_C);
  409.     gcv.foreground=colors[WILD_PIECE];
  410.     PiecesGC[WILD_PIECE]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  411.     PiecesPM[WILD_PIECE]=XCreateBitmapFromData(xw_display, xw_window,
  412.             jewel_bits, jewel_width, jewel_height);
  413.  
  414.     colors[PIECE1]=xw_alloc_color(PIECE1_C);
  415.     gcv.foreground=colors[PIECE1];
  416.     PiecesGC[PIECE1]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  417.     PiecesPM[PIECE1]=XCreateBitmapFromData(xw_display, xw_window,
  418.             piece1_bits, piece1_width, piece1_height);
  419.  
  420.     colors[PIECE2]=xw_alloc_color(PIECE2_C);
  421.     gcv.foreground=colors[PIECE2];
  422.     PiecesGC[PIECE2]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  423.     PiecesPM[PIECE2]=XCreateBitmapFromData(xw_display, xw_window,
  424.             piece2_bits, piece2_width, piece2_height);
  425.  
  426.     colors[PIECE3]=xw_alloc_color(PIECE3_C);
  427.     gcv.foreground=colors[PIECE3];
  428.     PiecesGC[PIECE3]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  429.     PiecesPM[PIECE3]=XCreateBitmapFromData(xw_display, xw_window,
  430.             piece3_bits, piece3_width, piece3_height);
  431.  
  432.     colors[PIECE4]=xw_alloc_color(PIECE4_C);
  433.     gcv.foreground=colors[PIECE4];
  434.     PiecesGC[PIECE4]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  435.     PiecesPM[PIECE4]=XCreateBitmapFromData(xw_display, xw_window,
  436.             piece4_bits, piece4_width, piece4_height);
  437.  
  438.     colors[PIECE5]=xw_alloc_color(PIECE5_C);
  439.     gcv.foreground=colors[PIECE5];
  440.     PiecesGC[PIECE5]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  441.     PiecesPM[PIECE5]=XCreateBitmapFromData(xw_display, xw_window,
  442.             piece5_bits, piece5_width, piece5_height);
  443.  
  444.     colors[PIECE6]=xw_alloc_color(PIECE6_C);
  445.     gcv.foreground=colors[PIECE6];
  446.     PiecesGC[PIECE6]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  447.     PiecesPM[PIECE6]=XCreateBitmapFromData(xw_display, xw_window,
  448.             piece6_bits, piece6_width, piece6_height);
  449.  
  450.     /* Flash pieces */
  451.     colors[FLASH1]=xw_alloc_color(FLASH_C);
  452.     gcv.foreground=colors[FLASH1];
  453.     PiecesGC[FLASH1]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  454.     PiecesPM[FLASH1]=XCreateBitmapFromData(xw_display, xw_window,
  455.             flash1_bits, flash1_width, flash1_height);
  456.  
  457.     colors[FLASH2]=colors[FLASH1];
  458.     PiecesGC[FLASH2]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  459.     PiecesPM[FLASH2]=XCreateBitmapFromData(xw_display, xw_window,
  460.             flash2_bits, flash2_width, flash2_height);
  461.  
  462.     colors[FLASH3]=colors[FLASH1];
  463.     PiecesGC[FLASH3]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  464.     PiecesPM[FLASH3]=XCreateBitmapFromData(xw_display, xw_window,
  465.             flash3_bits, flash3_width, flash3_height);
  466.  
  467.     colors[FLASH4]=colors[FLASH1];
  468.     PiecesGC[FLASH4]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  469.     PiecesPM[FLASH4]=XCreateBitmapFromData(xw_display, xw_window,
  470.             flash4_bits, flash4_width, flash4_height);
  471.  
  472.     /* Backdrops */
  473.     colors[BACKGND1]=xw_alloc_color(BACKGND_C);
  474.     gcv.foreground=colors[BACKGND1];
  475.     PiecesGC[BACKGND1]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  476.     PiecesPM[BACKGND1]=XCreateBitmapFromData(xw_display, xw_window,
  477.             back1_bits, back1_width, back1_height);
  478.  
  479.     colors[BACKGND2]=colors[BACKGND1];
  480.     PiecesGC[BACKGND2]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  481.     PiecesPM[BACKGND2]=XCreateBitmapFromData(xw_display, xw_window,
  482.             back2_bits, back2_width, back2_height);
  483.  
  484.     colors[BACKGND3]=colors[BACKGND1];
  485.     PiecesGC[BACKGND3]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  486.     PiecesPM[BACKGND3]=XCreateBitmapFromData(xw_display, xw_window,
  487.             back3_bits, back3_width, back3_height);
  488.  
  489.     colors[BACKGND4]=colors[BACKGND1];
  490.     PiecesGC[BACKGND4]=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  491.     PiecesPM[BACKGND4]=XCreateBitmapFromData(xw_display, xw_window,
  492.             back4_bits, back4_width, back4_height);
  493.  
  494.     /* fonts */
  495.     gcvm|=GCFont;
  496.  
  497.     if ( (VerFont=XLoadQueryFont(xw_display,VER_FONT)) == NULL)
  498.         { xw_fatal("Cannot load VERSION font.\n",__LINE__,__FILE__); }
  499.     gcv.font=VerFont->fid;
  500.     gcv.foreground=white;
  501.     VerGC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  502.  
  503.     if ( (HighScoreFont=XLoadQueryFont(xw_display,HIGHSC_FONT)) == NULL)
  504.         { xw_fatal("Cannot load HIGHSCORE font.\n",__LINE__,__FILE__); }
  505.     gcv.foreground=white;
  506.     gcv.font=HighScoreFont->fid;
  507.     HighScoreGC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  508.  
  509.     if ( (ScoreFont=XLoadQueryFont(xw_display,SCORE_FONT)) == NULL)
  510.         {
  511.         if ( (ScoreFont=XLoadQueryFont(xw_display,ALT_SC_FONT)) == NULL)
  512.             { xw_fatal("Cannot load SCORE font.\n",__LINE__,__FILE__); }
  513.         }
  514.     gcv.foreground=white;
  515.     gcv.font=ScoreFont->fid;
  516.     ScoreGC=XCreateGC(xw_display, xw_window, gcvm, &gcv);
  517.     Score_Char_MHeight=(ScoreFont->ascent + ScoreFont->descent)*3/2;
  518.     Score_x_right=SCORE_LOC_X + (ScoreFont->max_bounds.width*SCORE_WIDTH);
  519.     }
  520.  
  521.  
  522.  
  523. int main(argc,argv)
  524. int argc;
  525. char **argv;
  526.     {
  527.     XKeyboardControl Values, savedValues;
  528.     struct timeval curtime;
  529.  
  530.     gettimeofday(&curtime,NULL);
  531.     srandom((unsigned int)(curtime.tv_usec>>8));
  532.  
  533.     JewelState=NULL_STATE;
  534.  
  535.     xw_init(argc,argv,SCREEN_X, SCREEN_Y);
  536.  
  537.     Init_Jewel();
  538.  
  539.     XGetKeyboardControl(xw_display,&savedValues);
  540.     Values.bell_duration=50;
  541.     XChangeKeyboardControl(xw_display,KBBellDuration, &Values);
  542.     xw_start();
  543.     Init_Draw_High_Scores();
  544.     Init_Intro();
  545.     Init_Help();
  546.  
  547.     xw_main_loop();
  548.     XChangeKeyboardControl(xw_display,KBBellDuration, &savedValues);
  549.     XCloseDisplay(xw_display);
  550.     exit(0);
  551.     }
  552.